package com.atwl.school_system.controller;

import com.atwl.school_system.bean.ProInfo;
import com.atwl.school_system.bean.vo.ProInfoVo;
import com.atwl.school_system.service.ProService;
import com.atwl.school_system.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProController {

    @Autowired
    private ProService proService;

    /**
     * 只能根据分类查找 和 价格进行排序
     *
     * @param id
     * @param category 分类
     * @param sort     价格排序 0 升序 1 降
     */
    @GetMapping("/cli/showpro")
    public R getProInfoById(@RequestParam(value = "id", required = false) String id, @RequestParam(value = "cate", required = false) String category,
                            @RequestParam(value = "sort", defaultValue = "0") Integer sort) {
        List<ProInfoVo> proInfoVo = proService.getProInfo(id, category, sort);
        return R.ok().setData(proInfoVo);
    }

    @GetMapping("/cli/showpro/all")
    public R getProInfo() {
        List<ProInfoVo> list = proService.getProInfoAll();
        return R.ok().setData(list);
    }

    /**
     * 商品上架
     */
    @PutMapping("/cli/uppro")
    public R upPro(@RequestBody ProInfoVo proInfoVo) {
        proService.upPro(proInfoVo);
        return R.ok();
    }

    /**
     * 获取最热的 前三个
     */
    @GetMapping("/cli/hot")
    public R searchHot(@RequestParam(value = "limit",defaultValue = "3") Integer limit) {
        List<ProInfoVo> proInfo = proService.getProHot(limit);
        return R.ok().setData(proInfo);
    }

    /**
     * 模糊匹配数据
     */
    @GetMapping("/cli/show/like")
    public R searchForName(@RequestParam("keywords") String keyWords, @RequestParam("limit") Integer num) {
        List<ProInfo> proInfo = proService.searchForName(keyWords, num);
        return R.ok().setData(proInfo);
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/cli/delete")
    public R deleteProduct(@RequestParam("id") String pid) {
        proService.deleteProduct(pid);
        return R.ok();
    }
}
